package de.dailab.prefgen;

import java.util.Collection;

public class Utils {

	/**
	 * Returns if a Type is a leaf Element
	 * 
	 * @param type
	 * @return
	 */
	public static boolean isElement(Class type) {

		return String.class.isAssignableFrom(type)
				|| Integer.class.isAssignableFrom(type) || type.isEnum()
				|| type.isPrimitive();
	}
	
	public static boolean isCollection(Class type) {
		return type.toString().contains("Collection")
				|| Collection.class.isAssignableFrom(type);
	}

	/**
	 * STRING STUFF
	 */

	public static String splitCamalCase(String s) {
		StringBuffer sb = new StringBuffer(s.length());

		if (s.length() > 0)
			sb.append(Character.toUpperCase(s.charAt(0)));

		for (int i = 1; i < s.length(); i++) {
			char c = s.charAt(i);
			if (Character.isUpperCase(c)) {
				sb.append(' ');
				sb.append(Character.toLowerCase(c));
			} else
				sb.append(c);
		}
		
		return sb.toString();
	}

	public static String stripPrefix(String name) {
		
		if (name.matches("(get|set|add)+[A-Z]+.*")){
			return name.replaceFirst("(get|set|add)", "");
		}
		return name;
	}

}
